using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GlynnTucker.Cache;
using System.Collections;

/// <summary>
/// Summary description for BasePage
/// </summary>
public class BasePage : System.Web.UI.Page 
{
    private string m_mainMenu;
    private string m_relatedLinks;
    private string m_footerMainMenu;
    private string m_mainTitle;
    private string m_mainContent;
    private string m_bannerLink;

    public string BannerLink {
        get {
            return m_bannerLink;
        }
        set {
            m_bannerLink = value;
        }
    }

    public string MainContent {
        get {
            return m_mainContent;
        }
        set {
            m_mainContent = value;
        }
    }

    public string MainTitle {
        get {
            return m_mainTitle;
        }
        set {
            m_mainTitle = value;
        }
    }

    public string FooterMainMenu {
        get {
            return m_footerMainMenu;
        }
        set {
            m_footerMainMenu = value;
        }
    }

    public string RelatedLinks {
        get {
            return m_relatedLinks;
        }
        set {
            m_relatedLinks = value;
        }
    }

    public string MainMenu {
        get {
            return m_mainMenu;
        }
        set {
            m_mainMenu = value;
        }
    }
	public BasePage()
	{
		//
		// TODO: Add constructor logic here
		//
        //TODO: Load menu
	}

    #region Cache controler
    /// <summary>
    /// Add or update cache
    /// </summary>
    /// <param name="key"></param>
    /// <param name="value"></param>
    private void AddToCache(string key, object value) {
        SimpleMemoryCache cache;
        if (Application["SystemCache"] == null) {
            cache = new SimpleMemoryCache();
            Application["SystemCache"] = cache;
        } else {
            cache = (SimpleMemoryCache)Application["SystemCache"];
        }

        if (!cache.Contains(key)) {
            cache.Add(key, value, DateTime.Now.AddMinutes(60)); //cache duration: 60 mins
        } else {
            cache.Update(key, value, DateTime.Now.AddMinutes(60)); //cache duration: 60 mins
        }
        return;
    }
    /// <summary>
    /// Get value from cache
    /// </summary>
    /// <param name="key"></param>
    /// <returns>NULL if not in cache or time is exprired, otherwise, return cached value</returns>
    private object GetFromCache(string key) {
        SimpleMemoryCache cache;
        if (Application["SystemCache"] == null) {
            cache = new SimpleMemoryCache();
            Application["SystemCache"] = cache;
        } else {
            cache = (SimpleMemoryCache)Application["SystemCache"];
        }

        if (!cache.Contains(key)) {
            //not in cache or cache time is exprired
            return null;
        } else {
            return cache.Get(key);
        }
    }
    #endregion Cache controller

    public string ImagePath {
        get {
            string result = "";
            object tmp = GetFromCache("ImagesFolder");
            if (tmp != null) {
                result = (string)tmp;
            } else {
                result = ConfigurationManager.AppSettings["ImagesFolder"];
                AddToCache("ImagesFolder", result);
            }
            return result;
        }
    }

    public int PagingSize {
        get {
            int result = 0;
            object tmp = GetFromCache("PagingSize");
            if (tmp != null) {
                result = (int)tmp;
            } else {
                string val = ConfigurationManager.AppSettings["PagingSize"];

                result = Convert.ToInt32(val);
                AddToCache("PagingSize", result);
            }
            return result;
        }
    }

}
